<template>
  <div class="content">
    <div class="header">
      <h2 class="title">{{ contentConfig.header.title }}</h2>
      <el-button type="primary" @click="handleCreateUserBtnClick" v-if="isCreate">
        {{ contentConfig.header.btnTitle }}
      </el-button>
    </div>
    <div class="table">
      <el-table :data="pageList" border style="width: 100%" v-bind="contentConfig.childrenTree">
        <template v-for="(item, index) in contentConfig.propsList" :key="index">
          <template v-if="item.type === 'custom'">
            <el-table-column align="center" v-bind="item">
              <template #default="scope">
                <!-- 添加一个作用域插槽,供外部自定义 -->
                <slot :name="item.slotName" v-bind="scope" :prop="item.prop"></slot>
              </template>
            </el-table-column>
          </template>
          <template v-else-if="item.type === 'handler'">
            <el-table-column label="操作" width="150px" align="center">
              <template #default="scope" v-if="isUpdate">
                <el-button
                  link
                  size="small"
                  type="primary"
                  icon="Edit"
                  @click="handleEditBtnClick(scope.row)"
                  >编辑</el-button
                >
                <el-button
                  link
                  size="small"
                  type="danger"
                  icon="Delete"
                  @click="handleDeleteBtnClick(scope.row.id)"
                  v-if="isDelete"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </template>
          <template v-else>
            <el-table-column v-bind="item" align="center" />
          </template>
        </template>
      </el-table>
    </div>
    <div class="pagination">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[10, 20, 30, 40]"
        layout="jumper, sizes, prev, pager, next, total"
        :total="pageTotalCount"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script setup lang="ts">
import useSystemStore from '@/store/main/system/system'
import { storeToRefs } from 'pinia'
import { ref } from 'vue'
import type { IProps } from './type'
import { usePermission } from '@/hooks/usePermissions'

const props = defineProps<IProps>()
const emit = defineEmits(['createBtnClick', 'editBtnClick', 'ok'])

// 获取是否有对应的操作权限
const isCreate = usePermission(`${props.contentConfig.pageName}:create`)
const isDelete = usePermission(`${props.contentConfig.pageName}:delete`)
const isUpdate = usePermission(`${props.contentConfig.pageName}:update`)

const systemStore = useSystemStore()

// 获取 userList 数据进行展示
const { pageList, pageTotalCount } = storeToRefs(systemStore)

// 页码相关逻辑
const currentPage = ref(1)
const pageSize = ref(10)
const handleSizeChange = () => {
  fetchPageListData()
}

const handleCurrentChange = () => {
  fetchPageListData()
}

// 分页请求函数
function fetchPageListData(formData: any = {}) {
  const size = pageSize.value
  const offset = (currentPage.value - 1) * size

  const queryInfo = { size, offset, ...formData }

  systemStore.postPageListAction(props.contentConfig.pageName, queryInfo)
}

fetchPageListData()

defineExpose({ fetchPageListData })

// 删除数据
async function handleDeleteBtnClick(id: number) {
  await systemStore.deletePageByIdAction(props.contentConfig.pageName, id)
  emit('ok')
}

// 编辑数据
function handleEditBtnClick(itemData: any) {
  emit('editBtnClick', itemData)
}

// 新建数据
function handleCreateUserBtnClick() {
  emit('createBtnClick')
}
</script>

<style lang="less" scoped>
.content {
  margin-top: 20px;
  padding: 20px;
  background: #fff;

  .header {
    display: flex;
    justify-content: space-between;
    align-items: flex-end;
    margin-bottom: 10px;

    .title {
      font-size: 22px;
    }
  }
}

.table {
  :deep(.el-table__cell) {
    padding: 12px 0;
  }

  .el-button {
    margin-left: 0;
    padding: 5px 8px;
  }
}

.pagination {
  display: flex;
  justify-content: flex-end;
  margin-top: 10px;
}
</style>
